Ext.onReady(function(){
    Ext.define('CompanyModel',{
        extend: 'Ext.data.Model',
        fields: [
			{name:'id',type:'string'},
            {name:'cn_name',type:'string'},
            {name:'en_name',type:'string'},
            {name:'registered_addr',type:'string'},
            {name:'registered_date',type: 'date'},
            {name:'registered_fund',type:'string'},
            {name:'corporation',type:'string'},
            {name:'telephone',type:'string'},
            {name:'parent__cn_name',type:'string'},
            {name:'parent__id',type:'string'}
        ]
    });
    //创建数据源
    var CompanyStore = Ext.create('Ext.data.Store', {
    	pageSize:20,
        model: 'CompanyModel',
	    proxy: {
	        type: 'ajax',
	        url: '/resourcecontrol/company_list/',
	        actionMethods:'post',
	        reader: {
	            type: 'json',
	            root: 'rows',
	            totalProperty: 'totalCount'
	        }
	    },
	    autoLoad: true
    });
    //创建Grid
    var CompanyGrid = Ext.create('Ext.grid.Panel',{
        store: CompanyStore,
        //selModel:Ext.create('Ext.selection.CheckboxModel'),
        columnLines:true,
        columns: [
            Ext.create('Ext.grid.RowNumberer'),// 序列      
            {text: "校区名称(中文)", width: 100, dataIndex: 'cn_name', sortable: false},
            {text: "校区名称(英文)", width: 100, dataIndex: 'en_name',sortable:false},
            {text: "开办时间", width: 80, dataIndex: 'registered_date', sortable: false,
            	renderer: Ext.util.Format.dateRenderer('Y-m-d')},
            {text: "注册资金(万)", width: 80, dataIndex: 'registered_fund', sortable: false},
            {text: "校区负责人", width: 80, dataIndex: 'corporation', sortable: false},
            {text: "联系电话", width: 100, dataIndex: 'telephone', sortable: false},
            {text: "上级校区", width: 80, dataIndex: 'parent__cn_name', sortable: false},
            {text: "校区地址", width: 280, dataIndex: 'registered_addr', sortable: false}
        ],
        layout : 'fit',
        loadMask:true,
        viewConfig: {
            stripeRows: true,
            listeners: {
                itemclick: function(dataview, record, item, index, e) {
    				
                },
                itemdblclick: function(dataview, record, item, index, e) {
                	var selected = CompanyGrid.getSelectionModel().getSelection();
        			if(selected!=''){
	        			form.getForm().reset();
						form.loadRecord(selected[0]);
	        			win.show();
	    			}else{
	    				Ext.Msg.alert('系统提示', '请选择需要修改的校区!');
	    			}	
                }
            }
        },
        dockedItems : [{
            xtype : 'pagingtoolbar',
            store : CompanyStore,
            dock : 'bottom',
            displayInfo : true,
            autoScroll:true 
        },{
        	dock:'top',
        	xtype:'toolbar',
        	items:[{
        		xtype:'button',
        		text:'添加',
        		iconCls:'add', 
        		handler:function(){
        			form.getForm().reset();
        			win.show();
        		}
        	},{
        		xtype:'button',
        		text:'修改',
        		iconCls:'edit', 
        		handler:function(){
        			var selected = CompanyGrid.getSelectionModel().getSelection();
        			if(selected!=''){
	        			form.getForm().reset();
						form.loadRecord(selected[0]);
	        			win.show();
	    			}else{
	    				Ext.Msg.alert('系统提示', '请选择需要修改的校区!');
	    			}	
        		}
        	},{
        		xtype:'button',
        		text:'删除',
        		iconCls:'delete', 
        		handler:function(){
        			var selected = CompanyGrid.getSelectionModel().getSelection();
        			if(selected !=''){
	        			Ext.MessageBox.confirm('提示', '确认删除吗?', function(btn){
	        				if(btn=='yes'){
			        			var ids=[];
			        			Ext.each(selected, function (item) {
			        				ids.push(item.data.id);
			        			});
			        			Ext.Ajax.request({
			        			    url:'/resourcecontrol/company_del/',
			        			    method : 'post',
			        			    params:{
			        			    	ids:ids.join(",")
			        			    },
			        			    success:function(response){
			        			        var results =  Ext.decode(response.responseText);
			        			        if(results.success){
			        			        	CompanyStore.load();
			        			        }else{
			        			        	Ext.Msg.alert('系统提示', results.message);
			        			        }
			        			    }
			        			});
	        				}
	        			});
        			}else{
        				Ext.Msg.alert('系统提示', '请选择需要删除的校区!');
        			}   
        		}
        	}]
        }]
    });
    

	var form = Ext.create('Ext.form.Panel', {
		width: 550,
		bodyPadding: 10,
        fieldDefaults: {
            labelAlign: 'right',
            labelWidth: 90,
            msgTarget: 'side'
        },
        layout: 'column',
		items : [{
			xtype:'hiddenfield',
			fieldLabel:'id',
			name:'id'
		},{
			xtype:'textfield',
			fieldLabel:'校区名称(中文)',
			name:'cn_name',
			//margin: '0 5 0 0',
			columnWidth: 1/2,
			allowBlank: false
		},{
			xtype:'textfield',
			fieldLabel:'校区名称(英文)',
			name:'en_name',
			//margin: '0 5 0 0',
			columnWidth: 1/2
		},{
			xtype:'textfield',
			fieldLabel:'校区地址',
			editable:false,
			name:'registered_addr',
			columnWidth: 1/1,
			allowBlank: false
		},{
			xtype:'datefield',
			fieldLabel:'开办时间',
			name:'registered_date',
			columnWidth: 1/2,
			format: 'Y-m-d',
			allowBlank: false
		},{
			xtype:'numberfield',
			fieldLabel:'注册资金(万)',
			name:'registered_fund',
			minValue: 0,
			value: 0,
			columnWidth: 1/2
		},{
			xtype:'textfield',
			fieldLabel:'校区负责人',
			name:'corporation',
			columnWidth: 1/2,
			allowBlank: false
		},{
			xtype:'textfield',
			fieldLabel:'联系电话',
			name:'telephone',
			columnWidth: 1/2
		},{
			xtype:'container',
			columnWidth: 1/2,
			items:[{
        		xtype: 'combobox',
            	name:'parent__id',
               	fieldLabel: '上级校区',
               	emptyText: '请选择校区',
               	editable:false,
               	store: new Ext.data.Store({
                   	singleton : true, 
        	   	    proxy: {
        	   	        type: 'ajax',
        	   	        url: '/resourcecontrol/company_select/',
        	   	        actionMethods:'post',
        	   	        reader: {
        	   	            type: 'json'
        	   	        }
        	   	    },
                   fields:['id', 'cn_name'],
                   autoLoad:true
               	}), 
				listeners: {  
					beforequery:function(queryEvent,eOpts){
						queryEvent.combo.getStore().load();
					} 
				},        	
               	loadingText:'正在加载数据，请稍侯……', 
               	triggerAction:'all', 
               	valueField:'id', 
               	displayField:'cn_name' 			
			}]
		}]
	});

	var win = Ext.create('Ext.window.Window', {
		modal: true,
		closeAction: 'hide',
		items : [form],
		plain : true,
		resizable : false,
		buttonAlign : 'center',
		buttons : [ {
			text : '保存',
			handler : function() {
				if (form.getForm().isValid()) {
					form.getForm().submit({
						submitEmptyText:false,
						waitTitle : '请稍候',
						waitMsg : '正在处理......',
						url : '/resourcecontrol/company_save/',
						success : function(form, action) {
							//Ext.Msg.alert('系统提示',  action.result.message);
							//form.getForm().reset();
							win.close();
							CompanyStore.load();
						},
						failure : function(form, action) {
							if (action.result) {
								Ext.Msg.alert('系统提示', action.result.message);
							} 
						}
					});
				}
			}
		}, {
			text : '取消',
			handler : function() {
				form.getForm().reset();
				//win.hide();
				win.close();
			}
		} ]
	});    

	
	
	Ext.create('Ext.container.Viewport',{
		layout:'fit',
		renderTo: 'company-grid',
		items:[CompanyGrid]
	});	
});